package com.ruoyi.web.core.config;

import com.eic.framework.jdbc.JdbcTemplateUtils;
import com.ruoyi.common.constant.DataSourceType;
import com.ruoyi.common.utils.StringUtils;
import liquibase.integration.spring.SpringLiquibase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

@Configuration
public class LiquibaseConfig {
    @Autowired
    private Environment environment;
    //是否执行数据库脚本
    @Value("${spring.liquibase.enable:true}")
    private boolean liquibaseRun;

    @Bean
    public SpringLiquibase masterLiquibase() {
        SpringLiquibase liquibase = new SpringLiquibase();
        liquibase.setDataSource(JdbcTemplateUtils.getDataSource(null));
        liquibase.setChangeLog("classpath:sql/master.xml");
        liquibase.setContexts(StringUtils.join(environment.getActiveProfiles(),","));
        liquibase.setShouldRun(liquibaseRun);
        return liquibase;
    }
    @Bean
    public SpringLiquibase demoLiquibase() {
        SpringLiquibase liquibase = new SpringLiquibase();
        liquibase.setDataSource(JdbcTemplateUtils.getDataSource(DataSourceType.DEMO));
        liquibase.setChangeLog("classpath:sql/demo.xml");
        liquibase.setContexts(StringUtils.join(environment.getActiveProfiles(),","));
        liquibase.setShouldRun(liquibaseRun);
        return liquibase;
    }
}
